package com.whut.whw.personalinfo.helpandfeedback;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.whut.whw.R;
import com.whut.whw.base.BaseActivity;
import com.whut.whw.login.LoginActivity;
import com.whut.whw.model.BackResult;
import com.whut.whw.post.PostGoodActivity;
import com.whut.whw.util.BackResultCallback;
import com.whut.whw.util.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import okhttp3.Call;

import static com.whut.whw.constant.ConfigConsts.BASE_URL;

/**
 * <pre>
 *     author : 杨丽金
 *     time   : 2017/06/27
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class HelpAndFeedbackActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @BindView(R.id.toolbar_title)
    TextView toolbar_title;

    @BindView(R.id.tv_feedback_submit)
    TextView tv_feedback_submit;

    @BindView(R.id.tv_feedback_content)
    EditText tv_feedback_content;

    @Override
    public int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initToolbar();
    }

    private void initToolbar() {
        mToolbar.setNavigationIcon(R.drawable.rc_back_icon);
       /* setSupportActionBar(mToolbar);
        ActionBar ab = getSupportActionBar();
        ab.setTitle(R.string.search_title);*/
        toolbar_title.setText(R.string.feedback_title);
        mToolbar.inflateMenu(R.menu.activity_helpandfeedback_nav_menu);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.helpandfeedback_activity_nav_record:
                        ToastUtils.showShort("反馈记录");
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void setListener() {
        tv_feedback_submit.setOnClickListener(new feedbackListener());
    }

    /**
     * 反馈界面发送按钮的监听发送反馈记录
     */
    private class feedbackListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
            //String userID="8a0ac4d45d2a07c7015d2a3b1ff20002";
            String userID = sp.getString("yhbh", "");
            if ("".equals(userID)) {
                ToastUtils.showShort("您尚未登录，请先登录");
                Intent intent = new Intent(HelpAndFeedbackActivity.this, LoginActivity.class);
                startActivity(intent);
            } else {
                String feedContent = tv_feedback_content.getText().toString();//标题
                if ("".equals(feedContent.trim())) {
                    ToastUtils.showShort("请输入反馈内容");
                    return;
                }
                String url = BASE_URL + "feedback/sentFeedback";
                PostFormBuilder postFormBuilder = OkHttpUtils.post();
                postFormBuilder.url(url);
                postFormBuilder.addParams("userID", userID)
                        .addParams("feedContent", feedContent)
                        .build()
                        .execute(new BackResultCallback() {
                              @Override
                               public void onError(Call call, Exception e, int id) {
                                    getUiDelegate().toastLong(e.toString());
                                    Log.e("反馈", e.toString());
                               }

                               @Override
                               public void onResponse(BackResult response, int id) {
                                    ToastUtils.showLong(response.getMsg());
                                    finish();
                               }
                });
            }
        }
    }
}
